<?php

namespace app\data\controller\report;

use app\data\service\CityUserRelationService;
use app\data\service\StringParse;
use think\admin\Controller;

/**
 * 数据统计-客户结算表
 * Class Customer
 * @package app\data\controller\report
 */
class Customer extends Controller
{
    /**
     * 绑定数据表
     * @var string
     */
    private $table = 'data_record';

    /**
     * 客户结算表
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function overviewIndex()
    {
        // 设置页面标题
        $this->title = '客户结算表';

        $date = request()->get('date');
        if (empty($date)) {
            for ($i = 0; $i <= 5; $i++) {
                $data[] = [
                    'month' => date('Y-m', strtotime("-$i months")),
                    'time' => date('Y-m-01 00:00:00', strtotime("-$i months"))
                ];
            }
        } else {
            $date = explode(' - ', $date);
            if ((strtotime($date[1]) - strtotime($date[0])) > 3600 * 24 * 31 * 6) {
                $this->error('时间范围不能超过6个月');
            }

            $firstDate = date('Y-m', strtotime($date[1]));
            $endDate = date('Y-m', strtotime($date[0]));
            for ($i = 0; $i <= 5; $i++) {
                $data[] = [
                    'month' => date('Y-m', strtotime("-$i months", strtotime($firstDate))),
                    'time' => date('Y-m-01 00:00:00', strtotime("-$i months", strtotime($firstDate)))
                ];

                if (date('Y-m', strtotime("-$i months", strtotime($firstDate))) == $endDate) {
                    break;
                }
            }
        }

        $this->list = $data ?? [];
        $this->fetch('overview_index');
    }

    /**
     * 客户结算表详情
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        // 设置页面标题
        $this->title = '客户结算表';
        // 创建查询对象
        $query = $this->_query($this->table)->order('id desc');

        $result = $query->like('phone,username,plate_number')->equal('province#query_province,query_city#city,painter')->dateBetween('date');

        $month = request()->get('month');
        $result->where('paint_status', 1)
            ->where('time_complete', '>=', $month . '-01 00:00:00')->where('time_complete', '<=', $month . '-31 23:59:59');

        // 数据查询分页
        if (input('output') == 'json') {
            $result = $result->page(true, false);
            $this->success('', $result);
        } else {
            $result->page();
        }
    }

    /**
     * 客户结算表详情导出
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function indexExport()
    {
        $this->index();
    }

    /**
     * 数据列表处理
     * @param array $data
     */
    protected function _indexExport_page_filter(array &$data)
    {
        $this->_page_filter($data);
    }

    /**
     * 数据列表处理
     * @param array $data
     */
    protected function _page_filter(array &$data)
    {
        foreach ($data as &$item) {
            [$item['car_vehicle_type_num'], $chinese] = StringParse::instance()->splitDigitsAndChinese($item['car_vehicle_type']);
        }
    }

    /**
     * 表单结果处理
     * @param boolean $state
     */
    protected function _form_result(bool $state, array $data)
    {
        if ($state) {
            $this->success('保存成功！');
        }
    }

    /**
     * 数据列表处理
     * @param array $data
     */
    protected function _teams_page_filter(array &$data)
    {
    }

    /**
     * 编辑动作
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit()
    {
        $this->_form($this->table, 'form');
    }

    /**
     * 运营商信息编辑
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function editOperate()
    {

    }

    /**
     * 对接人信息编辑
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function editDocking()
    {

    }

    /**
     * 表单数据处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _form_filter(array &$data)
    {
        $this->title = '客户结算表管理';
        if ($this->request->isPost()) {
        } else {

        }
    }

    /**
     * 删除
     * @throws \think\db\exception\DbException
     */
    public function remove()
    {
        $this->app->db->name($this->table)->where('id', input('id'))->delete();
        $this->success('删除成功');
    }
}